package com.sky.business.controller;

import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.sky.common.core.util.R;
import com.sky.common.core.util.SpringContextHolder;
import com.sky.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 验证码相关的接口
 */
@Inner(false)
@RestController
@RequestMapping("/code")
@Tag(description = "code", name = "验证码相关接口")
@RequiredArgsConstructor
public class SkyImageCodeController {


	private static final String IMAGE_CODE_TYPE = "blockPuzzle";

	/**
	 * 创建图形验证码
	 */
	@SneakyThrows
	@GetMapping("/image/create")
	public R<ResponseModel> image() {
		CaptchaVO vo = new CaptchaVO();
		vo.setCaptchaType(IMAGE_CODE_TYPE);
		CaptchaService captchaService = SpringContextHolder.getBean(CaptchaService.class);
		ResponseModel responseModel = captchaService.get(vo);
		return R.ok(responseModel);
	}

	@SneakyThrows
	@GetMapping("/image/check")
	@Parameters({@Parameter(name = "pointJson", description = "pointJson", required = true),
			@Parameter(name = "token", description = "token", required = true)})
	public R<ResponseModel> check(@RequestParam String pointJson, @RequestParam String token) {
		CaptchaVO vo = new CaptchaVO();
		vo.setPointJson(pointJson);
		vo.setToken(token);
		vo.setCaptchaType(IMAGE_CODE_TYPE);
		CaptchaService captchaService = SpringContextHolder.getBean(CaptchaService.class);
		return R.ok(captchaService.check(vo));
	}

}
